#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define b back
#define cout(x) cout << x << endl
#define lb long double
#define soa(arr, n) sort(arr,arr+n)
#define inputVector(v, n) for(ll i = 0; i < n; i++) cin >> v[i]
#define sov(v) sort(v.begin(), v.end())
#define rev(v) reverse(v.begin(), v.end())
#define pl pair<ll, ll>
#define all(a) a.begin(), a.end()
#define vpp vector<pair<ll, ll>>
#define fastIo ios_base::sync_with_stdio(false), cin.tie(0)
#define endl '\n'
#define No cout << "NO\n";
#define Yes cout << "YES\n";
typedef long long int ll;
ll P = 1e9 + 7;
long long solve() {
ll n, m;
cin >> n >> m;
vector<ll> v(n);
vector<ll> freq(n + m + 1);
vector<ll> firstOccurrence(n + m + 1);
for(ll i = 0; i < n; i ++) {
cin >> v[i];
firstOccurrence[v[i]] = 0;
}
ll a, b;
ll currRepititions = 0;
vector<ll> temp(n);
ll ans = 0;
for(ll i = 0; i < m; i ++) {
cin >> a >> b;
a--;
ll prevVal = v[a];
ll prevFreq = i + 1 - firstOccurrence[prevVal];
freq[prevVal] += prevFreq;
v[a] = b;
firstOccurrence[b] = i + 1;
currRepititions -= temp[a];
temp[a] = i + 1 - freq[b];
currRepititions += temp[a];
ans += currRepititions;
}
ll vals = (m + 1) * m;
vals /= 2;
cout << ans + vals * n << endl;
return 0;
}
int main() {
ll t=1,i=1, n;
fastIo;
cin >> t;
while(t--) {
solve();
i ++;
}
return 0;
}
528. Random Pick with Weight | 470. Implement Rand10() Using Rand7() |
866. Prime Palindrome | 1516A - Tit for Tat |
622. Design Circular Queue | 814. Binary Tree Pruning |
791. Custom Sort String | 787. Cheapest Flights Within K Stops |
779. K-th Symbol in Grammar | 701. Insert into a Binary Search Tree |
429. N-ary Tree Level Order Traversal | 739. Daily Temperatures |
647. Palindromic Substrings | 583. Delete Operation for Two Strings |
518. Coin Change 2 | 516. Longest Palindromic Subsequence |
468. Validate IP Address | 450. Delete Node in a BST |
445. Add Two Numbers II | 442. Find All Duplicates in an Array |
437. Path Sum III | 436. Find Right Interval |
435. Non-overlapping Intervals | 406. Queue Reconstruction by Height |
380. Insert Delete GetRandom O(1) | 332. Reconstruct Itinerary |
368. Largest Divisible Subset | 377. Combination Sum IV |
322. Coin Change | 307. Range Sum Query - Mutable |